
import gradio as gr
# SDK引入模型
from dwspark.config import Config
from dwspark.models import Audio2Text
# 日志
# 加载系统环境变量：SPARKAI_UID、SPARKAI_APP_ID、SPARKAI_API_KEY、SPARKAI_API_SECRET
config = Config()
model = Audio2Text(config)

def gen_audio(audio_path: str):
    '''
    大模型对话
    :param audio_path: 音频路径
    :return:
    '''

    audio_result = model.recognize(audio_path)
    return [{'role': 'assistant', 'content': audio_result}]


# gradio页面绘制
with gr.Blocks() as demo:
    # 聊天对话框
    audio = gr.Audio(type='filepath', label="上传语音文件")
    chatbot = gr.Chatbot([], elem_id="chat-box", label="识别结果", type='messages')
    # 按钮
    llm_submit_tab = gr.Button("语音识别", visible=True)
    # 按钮出发逻辑
    llm_submit_tab.click(fn=gen_audio, inputs=[audio], outputs=[chatbot])

if __name__ == "__main__":
    demo.queue().launch()
